import { _decorator, Component, find, Label, Node, tween, Tween, UITransform, Vec3 } from 'cc';
import { AudioManager, Audios } from '../Managers/AudioManager';
import Banner from '../../Banner';
import { Panel, UIManager } from '../Managers/UIManager';
const { ccclass, property } = _decorator;

@ccclass('CommonTipPanel')
export class CommonTipPanel extends Component {
    panel: Node = null;
    button_1: Node = null;
    button_2: Node = null;

    titleLb: Label = null;
    contentLb: Label = null;
    button_1_Lb: Label = null;
    button_2_Lb: Label = null;
    button_1_UT: UITransform = null;
    button_2_UT: UITransform = null;
    cb_1: Function = null;
    cb_2: Function = null;

    protected onLoad(): void {
        this.panel = find("Panel", this.node);
        this.button_1 = find("Panel/Buttons/Button_1", this.node);
        this.button_2 = find("Panel/Buttons/Button_2", this.node);
        this.titleLb = find("Panel/TitleLabel", this.node).getComponent(Label);
        this.contentLb = find("Panel/ContentLabel", this.node).getComponent(Label);
        this.button_1_Lb = find("Panel/Buttons/Button_1/Button_1_Label", this.node).getComponent(Label);
        this.button_2_Lb = find("Panel/Buttons/Button_2/Button_2_Label", this.node).getComponent(Label);
        this.button_1_UT = find("Panel/Buttons/Button_1", this.node).getComponent(UITransform);
        this.button_2_UT = find("Panel/Buttons/Button_2", this.node).getComponent(UITransform);
    }

    protected onDisable(): void {
        Tween.stopAllByTarget(this.panel);
        this.panel.setScale(0, 0, 0);
    }

    Show(content: string, button1Str: string = ``, button2Str: string = ``, cb_1: Function = null, cb_2: Function = null, title: string = `提示`) {
        Tween.stopAllByTarget(this.panel);
        this.panel.setScale(0, 0, 0);

        this.contentLb.string = content;
        this.titleLb.string = title;
        this.button_1_Lb.string = button1Str;
        this.button_2_Lb.string = button2Str;

        this.cb_1 = cb_1;
        this.cb_2 = cb_2;

        this.button_1.active = button1Str != null && button1Str.length !== 0;
        this.button_2.active = button2Str != null && button2Str.length !== 0;

        if (this.button_1.active) {
            this.scheduleOnce(() => {
                this.button_1_UT.setContentSize(this.button_1_Lb.node.getComponent(UITransform).contentSize.width + 125, this.button_1_UT.contentSize.height);
            });
        }

        if (this.button_2.active) {
            this.scheduleOnce(() => {
                this.button_2_UT.setContentSize(this.button_2_Lb.node.getComponent(UITransform).contentSize.width + 125, this.button_2_UT.contentSize.height);
            });
        }

        tween(this.panel).to(0.3, { scale: Vec3.ONE }, { easing: 'backOut' }).call(() => {
            if (Banner.NeedShowMoreAd) { Banner.Instance.ShowCustomAd(); }
        }).start();
    }

    OnButtonClick(event, data) {
        AudioManager.PlaySound(Audios.ButtonClick);

        switch (event.target.name) {
            case 'Button_1':
                this.cb_1 && this.cb_1();
                break;

            case 'Button_2':
                this.cb_2 && this.cb_2();
                break;

            case 'Mask':
                UIManager.GetInstance().HidePanel(Panel.CommonTipPanel);
                break;
        }
    }
}


